//
//  XMRealViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/12.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMRealViewController.h"
#import <AlipayVerifySDK/MPVerifySDKService.h>

@interface XMRealViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *numField;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@end

@implementation XMRealViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"实名认证" forState:UIControlStateNormal];
    self.view.backgroundColor = kHEX(@"F2F2F2");
    self.navView.backgroundColor = kHEX(@"F2F2F2");

    if (kUserModel.idcard_verify) {
        self.nameField.enabled = NO;
        self.numField.enabled = NO;
        self.sureButton.enabled = NO;
        self.nameField.text = kUserModel.realname;
        self.numField.text = kUserModel.idcard;
        [self.sureButton setTitle:@"已实名" forState:UIControlStateNormal];
    }
}

#pragma mark - Actions
- (IBAction)sureAction:(UIButton *)sender {
    if ([XMUtil isBlankString:self.nameField.text]) {
        [MBProgressHUD showHUDMsg:self.nameField.placeholder toView:kWindow];
        return;
    }
    
    if ([XMUtil isBlankString:self.numField.text]) {
        [MBProgressHUD showHUDMsg:self.numField.placeholder toView:kWindow];
        return;
    }
    
    [self.view endEditing:YES];
    
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"cert_name": self.nameField.text,
        @"cert_no": self.numField.text
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/user/identify/card" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            NSString *certify_id = [result toString:@"certify_id"];
            [self faceAuthenticationCertifyId:certify_id];
        }
    }];
    
}

- (void)faceAuthenticationCertifyId:(NSString *)certifyId {
    [[MPVerifySDKService sharedInstance] verifyWith:certifyId
                                         currentCtr:self
                                          extParams:@{ZIM_EXT_PARAMS_KEY_FACE_CIRCLE_COLOR: @"FE3277"}
                                       onCompletion:^(ZIMResponse *response) {
        if (response.code == ZIMResponseSuccess) {
            NSMutableDictionary *dic = @{
                @"cert_name": self.nameField.text,
                @"cert_no": self.numField.text,
                @"certify_id": certifyId
            }.mutableCopy;
            [YHNetworkManager.sharedInstance postWithUrl:@"api/user/identify/face" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
                if (tip.success) {
                    [MBProgressHUD hideHUD];
                    kUserModel.idcard_verify = YES;
                    [MBProgressHUD showHUDMsg:@"实名认证成功!" toView:kWindow];
                    [self backAction];
                }
            }];
        } else {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showHUDMsg:response.reason toView:nil];
        }
    }];
}
@end
